#include <wfcClient.h>
#include <wfcSession.h>
#include <wxyutils.h>
#include <wxyDrawing.h>
#include <wxyOpenDrw.h>
#include <ProToolkitErrors.h>
#include "main.h"

wfcStatus OpenDrw()
{
    pfcSession_ptr session = pfcGetProESession();
    try
    {
        pfcSelectionOptions_ptr opts = pfcSelectionOptions::Create("prt_or_asm");
        opts->SetMaxNumSels(1);
        pfcSelections_ptr sels = session->Select(opts);
        pfcModel_ptr Model;
        //未选择模型则打开当前模型工程图
        if (sels == NULL)
        {
            Model = session->GetCurrentModel();
        }
        else
        {
            Model = sels->get(0)->GetSelModel();
        }
        _OpenDrawing(Model);
    }
    OTK_EXCEPTION_PRINT_LOG
    return wfcTK_NO_ERROR;
}
wfcStatus _OpenDrawing(pfcModel_ptr Model)
{
    pfcSession_ptr Session = pfcGetProESession();
    xstring FullName = Model->GetFullName();

    pfcModel_ptr Drawing;
    //未检索到模型输出错误信息
    if (_RetrieveDrawing(Model, &Drawing))
    {
        Drawing->DisplayInNewWindow();
        pfcWindow_ptr Window = Session->GetModelWindow(Drawing);
        Session->SetCurrentWindow(Window);
        Window->Activate();
        return wfcTK_NO_ERROR;
    }
}
int _RetrieveDrawing(pfcModel_ptr Model, pfcModel_ptr *Drawing, xstring filepath)
{
    pfcSession_ptr Session = pfcGetProESession();

    xstring FullName = Model->GetFullName();
    pfcModelDescriptor_ptr ModelDesc = Model->GetDescr();
    pfcModelDescriptor_ptr DrawingDescr = pfcModelDescriptor::CreateFromFileName((FullName + ".drw"));
    if (filepath == xstringnil)
    {
        xstring ModelOrigin = Model->GetOrigin();
        _FileExt file(ModelOrigin);
        DrawingDescr->SetPath(xstring(file.filepath.c_str()));
    }
    else
        DrawingDescr->SetPath(filepath);
    pfcRetrieveModelOptions_ptr DrawingRet = pfcRetrieveModelOptions::Create();
    DrawingRet->SetAskUserAboutReps(false);
    try
    {
        *Drawing = Session->RetrieveModelWithOpts(DrawingDescr, DrawingRet);
        return 1;
    }
    xcatchbegin
        xcatch(pfcXToolkitError, pfcex)
    {
        pfcMessageDialogOptions_ptr opts = pfcMessageDialogOptions::Create();
        opts->SetDefaultButton(pfcMESSAGE_BUTTON_OK);
        xint E_code = pfcex->GetErrorCode();
        xstring msg = pfcex->GetMessage();
        msg += "\n";
        msg += FullName;
        switch (E_code)
        {
        case PRO_TK_E_NOT_FOUND:
            msg += ".drw does not exist, or was not found!";
            break;
        case PRO_TK_CANT_OPEN:
            msg += ".drw is not readable!";
            break;
        case PRO_TK_INVALID_NAME:
            msg += ".drw name is too long!";
            break;
        case PRO_TK_INVALID_FILE:
            msg += ".drw is not a Creo Parametric model!";
            break;
        case PRO_TK_NO_PERMISSION:
            msg += ".drw does not have permission to operate on this model";
            break;
        default:
            break;
        }
        Session->UIShowMessageDialog(msg, opts);
    }

    xcatchend catch (exception e)
    {
        LOG(LOG_ERROR) << "Msg:" << e.what();
    }

    return 0;
}